package com.luck.picture.lib.samsung;

import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Build;
import androidx.annotation.RequiresApi;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Created by Administrator on 2018/3/9.
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class SamSungVideo {
    private MediaExtractor mediaExtractor;
    private MediaMuxer mediaMuxer;
    private File srcFile;
    private File targetFile;

    public SamSungVideo(String name, String targetname) {
        srcFile = new File(name);
        targetFile = new File(targetname);
        if (targetFile.exists()) {
            targetFile.delete();
        }
    }

    public void muxerMedia() {
        if (srcFile.exists()) {
            try {
                mediaExtractor = new MediaExtractor();
                int audioExtractorIndex = -1;
                int videoExtractorIndex = -1;
                mediaExtractor.setDataSource(srcFile.getAbsolutePath());
                for (int i = mediaExtractor.getTrackCount() - 1; i >= 0; i--) {
                    if (mediaExtractor.getTrackFormat(i).getString(MediaFormat.KEY_MIME).startsWith("audio/")) {
                        audioExtractorIndex = i;
                    } else if (mediaExtractor.getTrackFormat(i).getString(MediaFormat.KEY_MIME).startsWith("video/")) {
                        videoExtractorIndex = i;
                    }
                }
                mediaMuxer = new MediaMuxer(targetFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
                int audioMuxerIndex = mediaMuxer.addTrack(mediaExtractor.getTrackFormat(audioExtractorIndex));
                int videoMuxerIndex = mediaMuxer.addTrack(mediaExtractor.getTrackFormat(videoExtractorIndex));
                mediaMuxer.start();

                write(audioExtractorIndex, audioMuxerIndex);
                write(videoExtractorIndex, videoMuxerIndex);

                mediaExtractor.release();
                mediaMuxer.release();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                mediaExtractor = null;
                mediaMuxer = null;
                srcFile.delete();
            }
        }
    }

    private void write(int ExtractorIndex, int MuxerIndex) {
        mediaExtractor.selectTrack(ExtractorIndex);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
        MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
        long SampleTime;
        long nowSampleTime = 0;
        while (true) {
            int readSampleSize = mediaExtractor.readSampleData(byteBuffer, 0);
            if (readSampleSize <= 0) {
                break;
            }
            bufferInfo.size = readSampleSize;
            bufferInfo.offset = 0;
            bufferInfo.flags = mediaExtractor.getSampleFlags();
            SampleTime = nowSampleTime == 0 ? 0 : Math.abs(mediaExtractor.getSampleTime() - nowSampleTime);
            nowSampleTime = mediaExtractor.getSampleTime();
            bufferInfo.presentationTimeUs += SampleTime;
            mediaMuxer.writeSampleData(MuxerIndex, byteBuffer, bufferInfo);
            mediaExtractor.advance();
        }
    }
}
